<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;

class HomeController extends Controller
{

    protected $oauth_site = 'http://blog.martin.net';
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
//        $this->middleware('auth');
    }

    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Contracts\Support\Renderable
     */
    public function index()
    {
        Cache::get();
        return view('home');
    }

    public function redirect(Request $request)
    {

         $query = http_build_query([
            'client_id' => 5,
            'redirect_uri' => 'http://blog.martin.net/callback',
            'response_type' => 'code',
            'scope' => '',
        ]);

        return redirect($this->oauth_site.'/oauth/authorize?'.$query);
    }


    public function callback(Request $req)
    {

        if ($req->code) {
            $http = new \GuzzleHttp\Client;
            $response = $http->post($this->oauth_site.'/oauth/token', [
                'form_params' => [
                    'grant_type' => 'authorization_code',
                    'client_id' => 5,
                    'client_secret' => 'LQWpbk67K1iQauL5CZ018jfwy8K8efQX9F6TYdka',
                    'redirect_uri' => 'http://blog.martin.net/callback',
                    'code' => $req->code,
                ],
            ]);
            \Log::info($response->getBody());
            return json_decode((string) $response->getBody(), true);
        } else {

            return ($req->url());
        }


    }







    /**
     * 隐式授权令牌  返回到客户
     */
    public function redirect2()
    {
        $query = http_build_query([
            'client_id' => 4,
            'redirect_uri' => 'http://blog.martin.net/callback',  //客户端链接
            'response_type' => 'token',
            'scope' => '',
        ]);

        return redirect($this->oauth_site.'/oauth/authorize?'.$query);
    }



}
